package com.donfelix.animationdemo.mainpage;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.donfelix.animationdemo.R;
import com.donfelix.animationdemo.animation.AnimationsActivity1;
import com.donfelix.animationdemo.common.Sample;
import com.donfelix.animationdemo.common.TransitionHelper;
import com.donfelix.animationdemo.databinding.RowSampleBinding;
import com.donfelix.animationdemo.reveal.RevealActivity;
import com.donfelix.animationdemo.share_elements.SharedElementActivity;
import com.donfelix.animationdemo.transitions.TransitionActivity1;

import java.util.List;

/**
 * Created by Don on 2017/3/22.
 * Desc
 */

public class SamplesRecyclerAdapter extends RecyclerView.Adapter<SamplesRecyclerAdapter.SampleViewHolder> {

    private Activity activity;
    private List<Sample> samples;

    public SamplesRecyclerAdapter(Activity activity, List<Sample> samples) {
        this.activity = activity;
        this.samples = samples;
    }

    @Override
    public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RowSampleBinding binding = RowSampleBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new SampleViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(final SampleViewHolder holder, final int position) {
        final Sample sample = samples.get(position);
        holder.binding.setSample(sample);
        holder.binding.sampleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (position) {
                    case 0:
                        transitionToActivity(TransitionActivity1.class, sample);
                        //ToastUtil.showToast(activity, "click " + holder.getAdapterPosition() + " item");
                        break;
                    case 1:
                        transitionToActivity(SharedElementActivity.class, holder, sample);
                        //ToastUtil.showToast(activity, "click " + holder.getAdapterPosition() + " item");
                        break;
                    case 2:
                        transitionToActivity(AnimationsActivity1.class, sample);
                        //ToastUtil.showToast(activity, "click " + holder.getAdapterPosition() + " item");
                        break;
                    case 3:
                        transitionToActivity(RevealActivity.class, holder, sample, R.string.transition_reveal1);
                        //ToastUtil.showToast(activity, "click " + holder.getAdapterPosition() + " item");
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void transitionToActivity(Class target, Sample sample) {
        Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, true);
        startActivity(target, pairs, sample);
    }

    private void transitionToActivity(Class target, SampleViewHolder holder, Sample sample) {
        Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, false,
                new Pair<>(holder.binding.sampleIcon, activity.getString(R.string.square_blue_name)),
                new Pair<>(holder.binding.sampleName, activity.getString(R.string.sample_blue_title)));
        startActivity(target, pairs, sample);
    }

    private void transitionToActivity(Class target, SampleViewHolder holder, Sample sample, int transitionName) {
        Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, false,
                new Pair<>(holder.binding.sampleIcon, activity.getString(transitionName)));
        startActivity(target, pairs, sample);
    }

    private void startActivity(Class target, Pair<View, String>[] pairs, Sample sample) {
        Intent intent = new Intent(activity, target);
        ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pairs);
        intent.putExtra("sample", sample);
        activity.startActivity(intent, compat.toBundle());
    }

    @Override
    public int getItemCount() {
        return samples == null ? 0 : samples.size();
    }

    public class SampleViewHolder extends RecyclerView.ViewHolder {
        RowSampleBinding binding;

        public SampleViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }
}
